#include "reporteditor.h"
#include "ui_reporteditor.h"
#include "reportwidget.h"
#include "srsettingsdialog.h"
#include "diagnostictemplatemodel.h"
#include "diagnostictemplatedelegate.h"

#include <QEventLoop>
#include <QIcon>
#include <QMenu>
#include <QSettings>
#include <QFileDialog>
#include <QScrollArea>
#include <QStyle>
#include <QMessageBox>
#include <QCloseEvent>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QDebug>

ReportEditor::ReportEditor(QWidget *parent) :
    modified(false),
    QWidget(parent),
    ui(new Ui::ReportEditor)
{
    ui->setupUi(this);
    setupToolBar();
    setupComponent();
    setupConnection();
}

ReportEditor::~ReportEditor()
{
    delete ui;
}

void ReportEditor::setupToolBar()
{
    QMenu *m;
    QAction *a;
    m = new QMenu(this);
    a = m->addAction(QIcon(":/png/undo.png"), tr("Revoke"), ui->reportor, SLOT(revokeComplete()));
    ui->completeBtn->setMenu(m);
    m = new QMenu(this);
    a = m->addAction(QIcon(":/png/undo.png"), tr("Revoke"), ui->reportor, SLOT(revokeVerify()));
    ui->verifyBtn->setMenu(m);
    m = new QMenu(this);
    a = m->addAction(QIcon(":/png/preview.png"), tr("Print preview"), ui->reportor, SLOT(printPreview()));
    a = m->addAction(tr("Print to PDF"), ui->reportor, SLOT(printToPDF()));
    ui->printBtn->setMenu(m);
}

void ReportEditor::setupComponent()
{
    tempModel = new DiagnosticTemplateModel(this);
    ui->tempView->setModel(tempModel);
    ui->tempView->setItemDelegate(new DiagnosticTemplateDelegate(this));
    ui->verifyBtn->setEnabled(false);
}

void ReportEditor::setupConnection()
{
    connect(ui->tempView, SIGNAL(doubleClicked(QModelIndex)),
            tempModel, SLOT(onDoubleClicked(QModelIndex)));
    connect(ui->tempView, SIGNAL(newItemAfter(QModelIndex)),
            tempModel, SLOT(onNewItemAfter(QModelIndex)));
    connect(ui->tempView, SIGNAL(newItemBefore(QModelIndex)),
            tempModel, SLOT(onNewItemBefore(QModelIndex)));
    connect(ui->tempView, SIGNAL(newSubItem(QModelIndex)),
            tempModel, SLOT(onNewSubItem(QModelIndex)));
    connect(tempModel, SIGNAL(editItemTitle(QModelIndex)),
            ui->tempView, SLOT(edit(QModelIndex)));
    connect(ui->tempView, SIGNAL(editItem(QModelIndex)),
            tempModel, SLOT(onEditItem(QModelIndex)));
    connect(tempModel, SIGNAL(editItem(QModelIndex,QString,QString,QString)),
            ui->tempView, SLOT(onEditItem(QModelIndex,QString,QString,QString)));
    connect(ui->tempView, SIGNAL(applyEdit(QModelIndex,QString,QString,QString)),
            tempModel, SLOT(onApplyEdit(QModelIndex,QString,QString,QString)));
    connect(ui->tempView, SIGNAL(removeItem(QModelIndex)),
            tempModel, SLOT(onRemoveItem(QModelIndex)));
    connect(tempModel, SIGNAL(itemSelected(QString,QString)),
            ui->reportor, SLOT(appendTemplateContents(QString,QString)));

    connect(ui->saveBtn, SIGNAL(clicked(bool)), ui->reportor, SLOT(saveReport()));
    connect(ui->completeBtn, SIGNAL(clicked(bool)), ui->reportor, SLOT(completeReport()));
    connect(ui->verifyBtn, SIGNAL(clicked(bool)), ui->reportor, SLOT(verifyReport()));
    connect(ui->printBtn, SIGNAL(clicked(bool)), ui->reportor, SLOT(print()));
    connect(ui->paperCombo, SIGNAL(currentTextChanged(QString)), ui->reportor, SLOT(setPaper(QString)));

    connect(ui->thumbnail, SIGNAL(imageActived(QPixmap)), ui->reportor, SLOT(insertImage(QPixmap)));
    connect(ui->reportor, SIGNAL(reportModified(bool)), this, SLOT(onReportModified(bool)));
    connect(ui->reportor, SIGNAL(reportCompleted(bool)), this, SLOT(onReportCompleted(bool)));
    connect(ui->reportor, SIGNAL(reportVerified(bool)), this, SLOT(onReportVerified(bool)));
    connect(ui->reportor, SIGNAL(titleChanged(QString)), this, SLOT(onTitleChanged(QString)));
    connect(ui->reportor, SIGNAL(reportSave(QStringList)), this, SIGNAL(reportSave(QStringList)));
    connect(ui->reportor, SIGNAL(postReport(QByteArray)), this, SIGNAL(postReport(QByteArray)));
    connect(ui->reportor, SIGNAL(downloadImages(QStringList)), this, SIGNAL(downloadImages(QStringList)));
}

void ReportEditor::openReport(const QByteArray &json)
{
    ui->thumbnail->clear();
    ui->reportor->openReport(json);
}

void ReportEditor::openReportFile(const QString &json)
{
    ui->thumbnail->clear();
    ui->reportor->openReportFile(json);
}

void ReportEditor::loadImage(const QString &png)
{
    ui->thumbnail->loadImage(png);
    ui->reportor->loadImage(png);
}

void ReportEditor::setCacheDir(const QString &dir)
{
    ui->reportor->setCacheDir(dir);
}

void ReportEditor::clearReportImages()
{
    ui->reportor->clearImages();
}

void ReportEditor::setReadOnly(bool yes)
{
    ui->reportor->setReadOnly(yes);
}

void ReportEditor::setStatus(int status)
{
    ui->reportor->setStatus(status);
}

void ReportEditor::on_settingsBtn_clicked()
{
    SRSettingsDialog dialog(this);
    if (QDialog::Accepted == dialog.exec()) {
        ui->reportor->reflushReportFormat();
    }
}

void ReportEditor::closeEvent(QCloseEvent *e)
{
    if (modified) {
        QMessageBox::StandardButtons button = QMessageBox::question(this, tr("Close Structured Report"),
                                                                    tr("Report has been modified, save it?"),
                                                                    QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
        if (QMessageBox::Yes == button) {
            ui->reportor->saveReport();
            e->accept();
        } else if (QMessageBox::Cancel == button) {
            e->ignore();
        }
    }
}

void ReportEditor::onReportCompleted(bool yes)
{
    ui->completeBtn->setDisabled(yes);
    ui->verifyBtn->setEnabled(yes);
}

void ReportEditor::onReportVerified(bool yes)
{
    //ui->verifyBtn->setDisabled(yes);
}

void ReportEditor::onTitleChanged(const QString &title)
{
    setWindowTitle(windowTitle() + "-" + title);
}

void ReportEditor::onReportNotify(const QByteArray &json)
{
    qDebug() << QString::fromUtf8(json);
    QJsonObject js = QJsonDocument::fromJson(json).object();
    int code = js["code"].toInt();
    if (code == 0) {
        QMessageBox::information(this, tr("Report Notify"), js["data"].toString());
    } else {
        QMessageBox::critical(this, tr("Report Notify"), js["data"].toString());
    }
}

void ReportEditor::on_imageBtn_clicked()
{
    QPixmap p = ui->thumbnail->getCurrentPixmap();
    if (!p.isNull()) ui->reportor->insertImage(p);
}
